//
//  ZYTabBar.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/9.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
// MARK: -  01 声明协议／声明方法，下步02 声明代理
protocol ZYTabBarDelegate: NSObjectProtocol{
    
    func tabBarButtonClick()
}



class ZYTabBar: UITabBar {

// MARK: - 02 声明代理
   weak var btnDelegate:ZYTabBarDelegate?   //weak  修饰
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        print(NSStringFromCGRect(self.frame))//{{0, 0}, {0, 0}}//奇怪，是0
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let wid = UIScreen.mainScreen().bounds.size.width/5
        var idx:CGFloat = 0
        for value in self.subviews{
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.origin.x = idx * wid
                value.bounds.size.width = wid
                if idx==1 { idx++}
                idx++
            }
        }

        composeButton.center.x = self.bounds.size.width * 0.5
        composeButton.center.y = self.bounds.size.height * 0.5

        
    }
    
    func setUI(){
        //添加    中间 撰写button
        addSubview(composeButton)
        
    }
    
    //懒加载 －个 button
    lazy var composeButton:UIButton = {
    let composeBtn = UIButton()
    composeBtn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置背景图
    composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        //设置图片
    composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //设置大小
    composeBtn.sizeToFit()

    return composeBtn
    }()
  // MARK: - 03 调用代理方法
    func buttonClick(){
        btnDelegate?.tabBarButtonClick()
    }
    
    
}
